import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: () => import('@/views/home/index')
    },
    {
      path: '/login',
      component: () => import('@/views/login/login'),
      hidden: true
    },
    {
      path: '/register',
      component: () => import('@/views/register/register')
    },
    {
      path: '/users',
      name: 'users',
      component: () => import('@/views/users/index')
    },
    {
      path: '/users/detail',
      component: () => import('@/views/users/detail')
    },
    {
      path: '/layout',
      component: () => import('@/views/layout/layout')
    }
  ],
  mode: 'history',
  linkActiveClass: 'active'
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = localStorage.getItem('token')
    if (token === null || token === '') {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
